﻿using Data;
using Level.Decorations;
using System;
using UnityEngine;

namespace Hero
{
    public class HeroScore : MonoBehaviour
    {
        public event Action Updated;

        private GameSceneData _data;
        private HeroState _state;

        public void Construct(GameSceneData data, HeroState state)
        {
            _data = data;
            _state = state;
            SubscribeToObstacles();
        }

        public void SetScore(int value)
        {
            _state.CurrentScore += value;
            Updated?.Invoke();
        }

        private void SubscribeToObstacles()
        {
            foreach (var obstacle in _data.Obstacles)
            {
                if (obstacle.TryGetComponent<ObstacleDeath>(out var obstacleDeath))
                    obstacleDeath.Removed += SetScore;
            }
        }
    }
}